package bktech.soict.hust.bknote.activity.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import bktech.soict.hust.bknote.R;
import bktech.soict.hust.bknote.activity.MainActivity;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Search extends Activity {
	List<smsMembers> aSMS = new ArrayList<smsMembers>();
	ArrayAdapter<smsMembers> adapter = null;
	ListView list;
	EditText edit;
	Button btSearch;
	SmsHelper helper = MainActivity.helper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		edit = (EditText) findViewById(R.id.edit_search);
		btSearch = (Button) findViewById(R.id.button_search);
		list = (ListView) findViewById(R.id.search_list);
		adapter = new searchAdapter();
		list.setAdapter(adapter);
		btSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Thread threadSearch = new Thread(new Runnable() {

					public void run() {
						String search = edit.getText().toString();
						runOnUiThread(new Runnable() {

							public void run() {
								adapter.clear();
								adapter.notifyDataSetChanged();
							}
						});

						if (search != "") {
							Cursor s = helper.querySearch();
							if (s.moveToFirst()) {
								for (int i = 1; i <= s.getCount(); i++) {
									final String body = s.getString(s
											.getColumnIndexOrThrow("body"));
									if (body.contains(search)) {
										final smsMembers thongTin = new smsMembers();

										final String display_name = s.getString(s
												.getColumnIndexOrThrow("display_name"));
										final String address = s.getString(s
												.getColumnIndexOrThrow("address"));
										final long time = s.getLong(s
												.getColumnIndexOrThrow("date"));

										runOnUiThread(new Runnable() {

											public void run() {
												thongTin.setText(body);
												if (display_name == null) {
													thongTin.setName(address);
												} else {
													thongTin.setName(display_name);
												}

												SimpleDateFormat formatter = new SimpleDateFormat(
														"hh:mm dd/MM");
												Calendar calendar = Calendar
														.getInstance();
												calendar.setTimeInMillis(time);
												String time_sms = formatter
														.format(calendar
																.getTime());
												thongTin.setTime(time_sms);
												adapter.add(thongTin);
											}
										});

									}
									s.moveToNext();
								}
							}
							s.close();
						}
					}
				});
				threadSearch.start();
			}

		});
	}

	class searchAdapter extends ArrayAdapter<smsMembers> {
		TextView name;
		TextView txt;
		ImageView image;

		public searchAdapter() {
			super(Search.this, android.R.layout.simple_list_item_1, aSMS);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			SearchHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater
						.inflate(R.layout.row_conversation, parent, false);
				holder = new SearchHolder(row);
				row.setTag(holder);
			} else {
				holder = (SearchHolder) row.getTag();
			}
			holder.populateFrom(aSMS.get(position));
			return row;
		}

	}

	static class SearchHolder {
		TextView name;
		TextView txt;
		ImageView image;
		TextView time;

		public SearchHolder(View row) {
			name = (TextView) row.findViewById(R.id.nameContact);
			txt = (TextView) row.findViewById(R.id.sms);
			time = (TextView) row.findViewById(R.id.timeConversation);
			image = (ImageView) row.findViewById(R.id.contactImage);
		}

		public void populateFrom(smsMembers r) {
			name.setText(r.getName());
			txt.setText(r.getText());
			time.setText(r.getTime());
			image.setImageResource(R.drawable.contact);
		}

	}

}
